package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.ElectromecEquipment;
import com.hd.pzhwlxt.service.ElectromecEquipmentService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-22 8:59
 * @author: 怀念天空
 * @Description: 机电设备信息
 */
@RestController
@RequestMapping("/elect")
public class ElectromecEquipmentController {

    @Autowired
    private ElectromecEquipmentService electromecEquipmentService;

    @GetMapping("/get")
    public Response<Object> getAllEquipment(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<ElectromecEquipment> result = electromecEquipmentService.list();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<ElectromecEquipment> result = electromecEquipmentService.page(new Page<>(pageNum,pageSize),null);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<ElectromecEquipment> getEquipmentInfoById(@PathVariable Integer id){
         ElectromecEquipment equipment = electromecEquipmentService.getById(id);
        if(equipment!=null){
            return Response.createSuccessResponse("查询成功", equipment);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addStaffInfo(@RequestBody ElectromecEquipment electromecEquipment){
        boolean save = electromecEquipmentService.save(electromecEquipment);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteEquipmentInfoByIds(@PathVariable Integer[] ids){
        boolean remove = electromecEquipmentService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateEquipmentInfo(@RequestBody ElectromecEquipment electromecEquipment){
        boolean update = electromecEquipmentService.updateById(electromecEquipment);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }
}
